package com.example.maple.ccsclient.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.RelativeLayout;

import com.example.maple.ccsclient.R;
import com.example.maple.ccsclient.model.PicModel;
import com.example.maple.ccsclient.utils.DisplayUtils;
import com.example.maple.ccsclient.utils.SharedPreferencesUtils;
import com.example.maple.ccsclient.view.OnItemClickListener;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

/**
 * Created by maple on 2016/7/24.
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

	//是否显示米字格
	boolean isShow;
	int mResource;
	List<PicModel> mList = null;
	Context mContext;
	int mColumn;



	private OnItemClickListener mOnItemClickListener;

	public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
		this.mOnItemClickListener = onItemClickListener;
	}



	public RecyclerViewAdapter(Context context, int resource, List<PicModel> list, int column) {
		mContext = context;
		mResource = resource;
		mList = list;
		mColumn = column;
		isShow = SharedPreferencesUtils.getIntersected(context);
	}


	//定义当前GridView是否在滑动状态
	private boolean scrollState = false;

	public void setScrollState(boolean scrollState) {
		this.scrollState = scrollState;
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		// 加载数据item的布局，生成VH返回
		View v = LayoutInflater.from(mContext).inflate(mResource, null);
		// 根据列数计算项目宽度，以使总宽度尽量填充屏幕
		int itemWidth = (mContext.getResources().getDisplayMetrics().widthPixels - (mColumn + 1) * DisplayUtils.dip2px(mContext, 2) - DisplayUtils.dip2px(mContext, 16)) / mColumn;
		// 下面根据比例计算您的item的高度，此处只是使用itemWidth
		int itemHeight = itemWidth;
		AbsListView.LayoutParams param = new AbsListView.LayoutParams(
				itemWidth,
				itemHeight);
		v.setLayoutParams(param);
		return new ViewHolder(v);
	}

	@Override
	public void onBindViewHolder(final ViewHolder holder, int position) {

		PicModel info = mList.get(position);
		if (isShow) {
			holder.intersected.setVisibility(View.VISIBLE);
		} else {
			holder.intersected.setVisibility(View.GONE);
		}
		if (null != info && !TextUtils.isEmpty(info.getCImages())) {

			if (!scrollState) {
				holder.mImageView.setImageURI(Uri.parse(info.getCImages()));
				if (mOnItemClickListener != null) {
					holder.mImageView.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							int pos = holder.getLayoutPosition();
							mOnItemClickListener.onItemClick(holder.itemView, pos);
						}
					});
				}
			} else {
				holder.mImageView.setImageURI(Uri.parse("http://img.blog.csdn.net/20151015163826688"));
			}

		}

	}

	@Override
	public int getItemCount() {
		return mList == null ? 0 : mList.size();
	}


	class ViewHolder extends RecyclerView.ViewHolder {

		SimpleDraweeView mImageView;
		//米字格
		RelativeLayout intersected;

		public ViewHolder(View itemView) {
			super(itemView);
			intersected = (RelativeLayout) itemView.findViewById(R.id.mizige);
			mImageView = (SimpleDraweeView) itemView.findViewById(R.id.my_image_view);
		}

	}


}
